package se.lnu.axez.physics.bullet;

import se.lnu.axez.IControl;
import se.lnu.axez.Transformation;

public final class RigidBody {
	private final IControl control;
	private final CollisionShape collisionShape;
	private final DiscreteDynamicsWorld world;
	private final float mass;
	private int rigidBodyId;

	public RigidBody(DiscreteDynamicsWorld world, IControl control, CollisionShape collisionShape, float mass) {
		this.world = world;
		this.control = control;
		this.collisionShape = collisionShape;
		this.mass = mass;
	}

	void create(BulletFacade bulletFacade, float[] tmpMatrix) {
		final Transformation transformation = control.getTransformation();
		transformation.copyTransformMatrix(tmpMatrix, 0);

		final int collisionShapeID = collisionShape.getCollisionShapeId();

		this.rigidBodyId = bulletFacade.createRigidBody(this.world.getWorldID(), collisionShapeID, tmpMatrix, mass);
		this.world.registerRigidBody(rigidBodyId, this);
	}
	
	void destroy(BulletFacade bulletFacade) {
		bulletFacade.destroyRigidBody(this.world.getWorldID(), rigidBodyId);
	}
	
	void update(float[] orientationMatrix) {
		Transformation transformation = control.getTransformation();
		if (transformation == null) {
			return;
		}
		
		transformation.setOrientation(orientationMatrix, 0);
//		
//		orientationComponent.setOrientation(orientationMatrix, 0);
	}
}
